<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-5
 * update Date 2013-11-6
 * <yl:channel type="cname"/>
 * 配合其他标签一起使用
 */
class YlChannelTaglib {
	public function parse($content,$arr=array(),$sid,$domain,$channel) {
        if (stripos($content, 'yl:channel') !== false) {
            $matches = preg_parse($content, 'yl:channel');
            foreach ($matches as $match) {
                $channelname = getParseOneAttr($match, 'channelname');
                $type = getParseOneAttr($match, 'type');
                $iscleartags = getParseOneAttr($match, 'isClearTags');
                $wordnum = getParseOneAttr($match, 'wordNum');
                $formatString = getParseOneAttr($match, 'formatString');
                $alt = getParseOneAttr($match, 'alt');
                $title = getParseOneAttr($match, 'title');
                $class = getParseOneAttr($match, 'class');
                $style = getParseOneAttr($match, 'style');

                if ($channelname) {
                    //add next 2013-12-11 增加单栏目标签缓存
                    if (($arr = S('cache_channelname_'.$sid.'_'.$channelname)) === false) {
                        $arr = M('cms_category')
                            ->field('(case when url <> "" then url when createurl <> "" then createurl
	                            else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
	                		 then catenaming else ("'.$channel['catenaming'].'") end),"{cateid}",id),"{@ChannelID}",id))) end) as url,
	                            replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,
	                            description')
                            ->where('siteid = '.$sid.' and cname="'.$channelname.'"')
                            ->find();
                        S('cache_channelname_'.$sid.'_'.$channelname, $arr);
                    }
                }

                if($type === 'CountOfContents') {
                    //$count = D('Article')->getCountContents($arr['id']);
                    //update next 2013-12-11  增加单栏目标签缓存
                    if (($arrcategory = S('cache_channel_'.$arr['id'])) === false) {
                        $arrcategory = M('cms_category')
                            ->field('(case when url <> "" then url when createurl <> "" then createurl
					                else (concat("'.$domain.'","/",replace(catenaming,"{cateid}",id))) end) as cateurl,
					                replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,
					                description,catetemplate,arttemplate,createurl,catenaming,artnaming,
					                inputtime,updatetime,content_num,pid')
                            ->where('siteid='.$sid.' and id='.$arr['id'])
                            ->find();
                        S('cache_channel_'.$arr['id'], $arrcategory);
                    }
                    $count=$arrcategory['content_num'];

                    $res = $count;
                } else {
                    $type = empty($type)?'cname':$type;
                    $contents = $arr[$type];
                    if ($iscleartags === 'true') {
                        $contents = strip_tags($contents);
                    }
                    if (isset($wordnum)) {
                        $res = common('String')->msubstr ( $contents, 0, $wordnum, 'utf-8', true, '' );
                    }else if(strtolower($type) === 'content'){
                        $res = str_replace("@/",$domain."/",$contents);
                    } else {
                        $res = $contents;
                    }
                    if (strtolower($type) === 'updatetime' || strtolower($type) === 'inputtime') {
                        $formatString = $formatString?$formatString:'Y-m-d';
                        $res = date($formatString,$contents);
                    }

                    //add next 2013-12-09 增加栏目标签，有存在type=imageurl的情况
                    if (strtolower($type) === 'imageurl'){
                        $valt=$alt;
                        $vtitle=$title;
                        $vclass=$class;
                        $vstyle=$style;
                        $res="<img src='".$contents."' alt='".$valt."' title='".$vtitle."' class='".$vclass."' style='".$vstyle."'/>";
                    }
                }
                $content = str_replace($match, $res, $content);
            }
        }
        return $content;
	}
}